$(function (){
    //加载默认隐藏展示页
     $(".show").hide()
    //单击后获取输入内容
    $(".btn").bind("click",function (){
        var text = $(".text").val();
        $.ajax({
            type: "post",
            url: "/nlp?action=emotion",
            data: {
                text: text
            },
            dataType: "json",
            success: function (data){
                console.log(data)
                var htmlStr = "";
                $.each(data.items,function(index,obj){
                    //情感极性结果
                    if(obj.sentiment === 0){
                        htmlStr +="<p >情感极性结果：<span style='color:red;'>负向</span></p>"
                    }else if(obj.sentiment  === 1){
                        htmlStr +="<p >情感极性结果：<span style='color:deepskyblue'>中性</span></p>"
                    }else{
                        htmlStr +="<p >情感极性结果：<span style='color:green;'>正向</span></p>"
                    }

                    //情感自信度
                    if(obj.confidence > 0 && obj.confidence < 0.3){
                        htmlStr +="<p>情感自信度：<span style='color: green'>"+obj.confidence+"</span></p>"
                    }else  if(obj.confidence > 0.3 && obj.confidence < 0.6){
                        htmlStr +="<p>情感自信度：<span style='color: deepskyblue'>"+obj.confidence+"</span></p>"
                    }else{
                        htmlStr +="<p>情感自信度：<span style='color: red'>"+obj.confidence+"</span></p>"
                    }

                    //情感积极率
                    if(obj.positive_prob> 0 && obj.positive_prob < 0.3){
                        htmlStr +="<p>情感积极率：<span style='color: red'>"+obj.positive_prob+"</span></p>"
                    }else  if(obj.positive_prob > 0.3 && obj.positive_prob < 0.6){
                        htmlStr +="<p>情感积极率：<span style='color: deepskyblue'>"+obj.positive_prob+"</span></p>"
                    }else{
                        htmlStr +="<p>情感积极率：<span style='color: green'>"+obj.positive_prob+"</span></p>"
                    }

                    //情感消极率
                    if(obj.negative_prob > -1 && obj.negative_prob < 0.3){
                        htmlStr +="<p>情感消极率：<span style='color: green'>"+obj.negative_prob+"</span></p>"
                    }else  if(obj.negative_prob > 0.3 && obj.negative_prob < 0.6){
                        htmlStr +="<p>情感消极率：<span style='color: deepskyblue'>"+obj.negative_prob+"</span></p>"
                    }else{
                        htmlStr +="<p>情感消极率：<span style='color: red'>"+obj.negative_prob+"</span></p>"
                    }

                })

                $(".show").html(htmlStr)
            },
            // error : function (err){
            //   console.log(err)
            // }
        })
        //单击后展示隐藏页面
        $(".show").show()
    })

})



